package com.myz.rabbitmq.confirm.batch;

import com.myz.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * confirm批量模式
 * channel.confirmSelect()
 * channel.waitForConfirms()
 *
 * @author maoyz on 18-4-11.
 */
public class ConfirmBatchSend {

    private final static String QUEUE_NAME = "confirm_batch_work_queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = ConnectionUtils.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 将当前channel设置成confirm模式,批量模式
        channel.confirmSelect();

        for (int i = 0; i < 10; i++) {
            String message = "Hello RabbitMQ" + i;
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        }

        if (!channel.waitForConfirms()) {
            System.out.println("发生异常...");
        } else {
            System.out.println("发送成功...");
        }
        channel.close();
        connection.close();

    }
}
